package com.foreverwin.jzymes.common.service.impl;

import com.foreverwin.jzymes.common.domain.CarrierBind;
import com.foreverwin.jzymes.common.domain.CncReqLog;
import com.foreverwin.jzymes.common.mapper.CncReqLogAdviceMapper;
import com.foreverwin.jzymes.common.service.ICarrierBindAdviceService;
import com.foreverwin.jzymes.common.service.ICncReqLogAdviceService;
import com.foreverwin.jzymes.framework.util.I18nUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

@Service
public class CncReqLogAdviceServiceImpl implements ICncReqLogAdviceService {

    @Autowired
    ICarrierBindAdviceService carrierBindAdviceService;
    @Autowired
    CncReqLogAdviceMapper cncReqLogAdviceMapper;

    /**
     * 查询本体设备信息
     *
     * @param site
     * @param carrier
     * @param date
     * @return
     */
    public List<CncReqLog> findCncReqLogByCreatedDate(String site, String carrier, Date date ){

        return findCncReqLogByCreatedDate( site, carrier, null, date );
    }

    public List<CncReqLog> findCncReqLogByCreatedDate(String site, String carrier, String resource, Date date ){
        Assert.hasText( site, I18nUtil.getI18nText( "common.siteNotBlank" ) );
        Assert.hasText( carrier, I18nUtil.getI18nText( "common.carrierNotBlank" ) );
        Assert.notNull( date, I18nUtil.getI18nText( "common.dateNotNull" ) );

        return cncReqLogAdviceMapper.findCncReqLogByCreatedDate( site, carrier, resource, date );
    }

    public CncReqLog findCncReqLogByBindDate( String site, String carrier, String resource ){

        /**检查本体是否绑定*/
        List<CarrierBind> carrierBindList = carrierBindAdviceService.existCarrierBindonBind( site, carrier );
        Date bindDate = carrierBindList.get( 0 ).getStartDateTime();

        List<CncReqLog> cncReqLogs = findCncReqLogByCreatedDate( site, carrier, resource, bindDate );
        if( cncReqLogs != null && cncReqLogs.size() > 0 ){
            return cncReqLogs.get( 0 );
        }
        return null;
    }

    public CncReqLog findCncReqLogByBindDate( String site, String carrier ){

        return findCncReqLogByBindDate( site, carrier, null );
    }

    /**
     * 查询自动线本体前五片本体
     *
     * @param site
     * @param carrier
     * @param resource
     * @return
     */
    public List<String> findPrevFiveCarrier( String site, String carrier, String resource ){
        List<String> carrierList = new ArrayList<>();
        int count = 5;

        CncReqLog cncReqLog = findCncReqLogByBindDate( site, carrier, resource );
        if( cncReqLog != null ){
            Date sendTime = cncReqLog.getSendTime();
            List<CncReqLog> cncReqLogs = cncReqLogAdviceMapper.findCncReqLogBySendDate( site, resource, sendTime );
            for( CncReqLog _cncReqLog : cncReqLogs ){
                List<CarrierBind> carrierBindList = carrierBindAdviceService.findCarrierBindOnBind( _cncReqLog.getSite(), _cncReqLog.getCarrier() );
                if( carrierBindList.size() > 0 ){
                    carrierList.add( _cncReqLog.getCarrier() );
                }
                count --;
                if( count == 0 ){
                    break;
                }
            }
        }

        return carrierList;
    }


    /**
     * 查询本体绑定设备
     *
     * @param site
     * @param carrier
     * @return
     */
    public String findCarrierBindResource( String site, String carrier ){

        CncReqLog cncReqLog = findCncReqLogByBindDate( site, carrier );
        if( cncReqLog != null ){
            return cncReqLog.getResrce();
        }
        return null;
    }

    /**
     * 汇总时间范围cnc设备产出
     *
     * @param site
     * @param startDate
     * @param endDate
     * @return
     */
    public List<Map<String,Object>> countCncYieldByDateRange( String site, Date startDate, Date endDate ){

        return cncReqLogAdviceMapper.countCncYieldByDateRange( site, startDate, endDate );
    }

    /**
     * 汇总时间范围cnc设备产出
     *
     * @param site
     * @param resource
     * @param startDate
     * @param endDate
     * @return
     */
    public Map<String,Object> countCncYield( String site, String resource, Date startDate, Date endDate ){

        return cncReqLogAdviceMapper.countCncYield( site, resource, startDate, endDate );
    }

}
